package com.pactera.jep.service.sys.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pactera.jep.commons.CollectionUtils;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.sys.code.DutySource;
import com.pactera.jep.sys.model.*;
import com.pactera.jep.sys.service.DutyService;
import com.pactera.jep.sys.service.StaffService;
import com.pactera.jep.sys.service.UnitService;
import com.pactera.jep.web.code.WebCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "duty")
@RequestMapping("/duty")
public class DutyRestController {
	
	@Autowired
	DutyService dutyService;

	@Autowired
	private UnitService unitService;

	@Autowired
	private StaffService staffService;

	@PostMapping("/select")
	public String select(String params) {

		Duty duty = new Duty();
		if(StringUtils.isNotBlank(params)){
			duty = JSONObject.parseObject(params, Duty.class);
		}
		String dutysString = null;
		List<Duty> dutys =   dutyService.select(duty);
		if(CollectionUtils.isNotEmpty(dutys)){
			dutysString = JSONArray.toJSON(dutys).toString();
		}
		return dutysString;
	}
	/**
	 * 保存，用户设定的角色
	 * @return
	 */
	@PostMapping(value = "/saveRoleStaff")
	@ApiOperation(value = "保存，用户设定的角色")
	public Map<String, Object> saveRoleStaff(@RequestBody JSONObject jsonData) {

		Map<String, Object> tips = new HashMap<String, Object>();

		try
		{
			String staffCode = jsonData.getString("staffCode");
			String unitCode = jsonData.getString("unitCode");
			String jsonRoleList = jsonData.getString("jsonRoleList");

			List<Role> roleList = JSON.parseArray(jsonRoleList, Role.class);
			Unit unit = unitService.get(unitCode);
			Staff staff = staffService.get(staffCode);
			dutyService.saveRoleList(roleList, staff, unit);


			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch (Exception e)
		{
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}

		return tips;
	}

	/**
	 * 保存为用户设定的角色组
	 * @return
	 */
	@PostMapping(value = "/saveRoleGroupStaff")
	@ApiOperation(value = "保存，用户设定的角色组")
	public Map<String, Object> saveRoleGroupStaff(@RequestBody JSONObject jsonData){

		Map<String, Object> tips = new HashMap<String, Object>();

		try
		{
			String staffCode = jsonData.getString("staffCode");
			String unitCode = jsonData.getString("unitCode");
			String jsonRoleGroupList = jsonData.getString("jsonRoleGroupList");
			/*删除之前设定的角色组*/
			List<Filter> filters = new ArrayList<>();
			filters.add(new Filter("staffCode", staffCode));
			filters.add(new Filter("dutySource", DutySource.ROLEGROUP.toString()));
			dutyService.deleteByFilter(filters);

			/*重新添加新设定的角色组*/
			List<RoleGroup> roleGroupList = JSON.parseArray(jsonRoleGroupList, RoleGroup.class);
			Unit unit = unitService.get(unitCode);
			Staff staff = staffService.get(staffCode);

			dutyService.saveRoleGroupList(roleGroupList, staff, unit);


			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch (Exception e)
		{
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}

		return tips;
	}

}
